<?php

namespace Mobi\Controller;

use Think\Controller;

class ApnController extends Controller
{
    const SEVER_PAR = "http://localhost/yubao_anal/Analysis/Api/getSmsReturn/";


    /**
     * 申请销售码
     */
    public function codeapply()
    {
        $mobile = I("post.phone");
        $msg = I("post.type") . "+" . I("post.name") . "+" . $mobile;
        if (!$this->existuser($mobile)) {
            $this->redirect("Apn/exits");
        }
        $temp = $this->openandresource($mobile, $msg);
        $ret = explode(",", $temp);
        if ($ret[0] == 1) {
            $this->redirect("Apn/succes", array("phone" => $mobile));
        }
        $this->redirect("Apn/err");
    }

    private function existuser($phone)
    {
        $salestaff = M("salestaff");
        $saleagent = M("saleagent");
        if ($salestaff->where("ssa_phone='" . $phone . "'")->count() > 0) {
            return false;
        }
        if ($saleagent->where("sag_phone='" . $phone . "'")->count() > 0) {
            return false;
        }
        return true;
    }

    /**
     * 更新个人资料
     */
    public function updateinfo()
    {
        $mobile = I("post.phone");
        $msg = I("post.code") . "+" . I("post.banktype") . "+" . I("post.bankname") . "+" . I("post.bankcard");

        $msgString  = join(I("post."),"+");
        $temp = $this->openandresource($mobile, $msg, $msgString);
        $ret = explode(",", $temp);
        if ($ret[0] == 1) {
            $this->redirect("Apn/upscc");
        }
        $this->error("资料有误，请重新绑定!");
    }

    /**
     * 申请通道
     * @param type $mobile
     * @param type $msg
     * @param string $exp
     * @return type
     */
    private function openandresource($mobile, $msg, $exp = "NULL")
    {
        $para = "receiver/getCulReturn/pswd/cul00011/mobile/";
        $para.= $mobile . "/msg/" . $msg."/exp/{$exp}";
        $para.= "/destcode/1069888888/isems/0/emshead/0/moTime/" . date("YmdHi") . ".html";
       # echo self::SEVER_PAR . $para;die;
        $ch = curl_init(self::SEVER_PAR . $para);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        return curl_exec($ch);
    }

    public function exits()
    {
        $this->display();
    }

    public function err()
    {
        $this->display();
    }

    public function succes($phone)
    {
        $salestaff = M("salestaff");
        $disisbn = M("disisbn");
        $info = $salestaff->where("ssa_phone=" . $phone)->find();
        if ($info == null) {
            $this->redirect("Apn/err");
        }
        $code = $disisbn->where("dis_ssaid=" . $info["ssa_id"])->find();
        $this->assign("info", $info);
        $this->assign("code", $code);
        $this->display();
    }

    public function upscc()
    {
        $this->display();
    }
}
